﻿/*
 * Created by SharpDevelop.
 * User: artem
 * Date: 27.04.2011
 * Time: 19:49
 * 
 */
using System;
using System.Collections.Generic;

using SettingManager;

namespace Kernel
{
	/// <summary>
	/// Класс исключений.
	/// Тупо для сепарирования ошибок нашего кода.
	/// </summary>
	public class OpenOrganizerException : Exception
	{
        private const string format_message_inner = @"{0}. Due to: {1}. ";
        private const string error_messages_config = @"ErrorMessages.conf";
        private const string default_error_message = @"Sorry, the reason is not known.";
        private static readonly Dictionary<string, string> error_messages = null;

		public enum ErrorCode : byte
		{
			TooLongTitle,
			TooShortTitle,
			TitleCanNotBeEmpty,
			DescriptionCanNotBeEmpty,
			TooLongDescription,
			DateCanNotBePast,
			InvalidNotificationInterval,
            IncorrectDataBase,
            CanNotGetEvent,
            CanNotGetEvents,
            CanNotAddEvent,
            CanNotUpdateEvent,
            CanNotDeleteEvent,
            DateBaseConnectError,
            FailDataBaseOperation,
            CanNotMakeSchedule,
            StartUpError,
            UndefinedError,
            ScheduleSavingFail,
            SchedulePrintFail,
		}

        static OpenOrganizerException()
        {
            try
            {
                XMLSettingManager settings = XMLSettingManager.Load(error_messages_config);

                error_messages = new Dictionary<string, string>();

                foreach (var it in Enum.GetNames(typeof(ErrorCode)))
                    error_messages.Add(it, settings[it]);
            }
            catch
            { }
        }

        public OpenOrganizerException(ErrorCode code)
            : base(GetMessage(code))
        { }

        public OpenOrganizerException(ErrorCode code, 
                                      Exception innerException)
            : base(String.Format(format_message_inner,
                                 GetMessage(code),
                                 innerException.Message), 
                   innerException)
        { }

        private static string GetMessage(ErrorCode code)
        {
            if (error_messages != null &&
                error_messages.ContainsKey(code.ToString()))
                return error_messages[code.ToString()];
            else
                return default_error_message;
        }
	}
}